home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / rhythmbox / plugins / magnatune / TrackListHandler.py < prev    next >
Encoding:
Python Source  |  2009-04-07  |  3.9 KB  |  116 lines

  1. # -*- Mode: python; coding: utf-8; tab-width: 8; indent-tabs-mode: t; -*-
  2. #
  3. # Copyright (C) 2006 Adam Zimmerman  <adam_zimmerman@sfu.ca>
  4. # Copyright (C) 2006 James Livingston  <doclivingston@gmail.com>
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2, or (at your option)
  9. # any later version.
  10. #
  11. # The Rhythmbox authors hereby grant permission for non-GPL compatible
  12. # GStreamer plugins to be used and distributed together with GStreamer
  13. # and Rhythmbox. This permission is above and beyond the permissions granted
  14. # by the GPL license by which Rhythmbox is covered. If you modify this code
  15. # you may extend this exception to your version of the code, but you are not
  16. # obligated to do so. If you do not wish to do so, delete this exception
  17. # statement from your version.
  18. #
  19. # This program is distributed in the hope that it will be useful,
  20. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22. # GNU General Public License for more details.
  23. #
  24. # You should have received a copy of the GNU General Public License
  25. # along with this program; if not, write to the Free Software
  26. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
  27.  
  28. import rhythmdb
  29. import xml.sax, xml.sax.handler
  30. import datetime
  31.  
  32. class TrackListHandler(xml.sax.handler.ContentHandler):
  33.  
  34.     def __init__(self, db, entry_type, sku_dict, home_dict, buy_dict, art_dict):
  35.         xml.sax.handler.ContentHandler.__init__(self)
  36.         self.__db = db
  37.         self.__entry_type = entry_type
  38.         self.__sku_dict = sku_dict
  39.         self.__home_dict = home_dict
  40.         self.__buy_dict = buy_dict
  41.         self.__art_dict = art_dict
  42.         self.__track = {}
  43.  
  44.     def startElement(self, name, attrs):
  45.         self.__text = ""
  46.  
  47.     def endElement(self, name):
  48.         if name == "Track":
  49.             try:
  50.                 # prefer ogg streams to mp3
  51.                 if 'oggurl' in self.__track:
  52.                     trackurl = self.__track['oggurl']
  53.                 else:
  54.                     trackurl = self.__track['url']
  55.     
  56.                 # add the track to the source
  57.                 entry = self.__db.entry_lookup_by_location (trackurl)
  58.                 if entry == None:
  59.                     entry = self.__db.entry_new(self.__entry_type, trackurl)
  60.  
  61.                 # if year is not set, use launch date instead
  62.                 try:
  63.                     year = parse_int(self.__track['year'])
  64.                     if year <= 0:
  65.                         raise ValueError
  66.                 except ValueError:
  67.                     year = parse_int(self.__track['launchdate'][0:4])
  68.  
  69.                 date = datetime.date(year, 1, 1).toordinal()
  70.                 try:
  71.                     tracknum = parse_int(self.__track['tracknum'])
  72.                 except ValueError:
  73.                     tracknum = 0
  74.                 try:
  75.                     duration = parse_int(self.__track['seconds'])
  76.                 except ValueError:
  77.                     duration = 0
  78.  
  79.                 self.__db.set(entry, rhythmdb.PROP_ARTIST, self.__track['artist'])
  80.                 self.__db.set(entry, rhythmdb.PROP_ALBUM, self.__track['albumname'])
  81.                 self.__db.set(entry, rhythmdb.PROP_TITLE, self.__track['trackname'])
  82.                 self.__db.set(entry, rhythmdb.PROP_TRACK_NUMBER, tracknum)
  83.                 self.__db.set(entry, rhythmdb.PROP_DATE, date)
  84.                 self.__db.set(entry, rhythmdb.PROP_GENRE, self.__track['mp3genre'])
  85.                 self.__db.set(entry, rhythmdb.PROP_DURATION, duration)
  86.  
  87.                 key = str(trackurl)
  88.                 sku = intern(str(self.__track['albumsku']))
  89.                 self.__sku_dict[key] = sku
  90.                 self.__home_dict[sku] = str(self.__track['home'])
  91.                 self.__buy_dict[sku] = str(self.__track['buy'].replace("buy_album", "buy_cd", 1))
  92.                 self.__art_dict[sku] = str(self.__track['cover_small'])
  93.  
  94.                 self.__db.commit()
  95.             except Exception,e: # This happens on duplicate uris being added
  96.                 print "Couldn't add %s - %s" % (self.__track['artist'], self.__track['trackname']), e
  97.  
  98.             self.__track = {}
  99.         elif name == "AllSongs":
  100.             pass # end of the file
  101.         else:
  102.             self.__track[name] = self.__text
  103.  
  104.     def characters(self, content):
  105.         self.__text = self.__text + content
  106.  
  107. # parses partial integers
  108. def parse_int(s):
  109.     news = ""
  110.     for c in s:
  111.         if c in '0123456789': # only positive integers allowed
  112.             news += c
  113.         else:
  114.             break
  115.     return int(news)
  116.